package com.tfswue.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfswue.group.entity.Order;
import com.tfswue.group.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/orders")
@Tag(name = "订单接口", description = "符合RESTful风格的订单管理接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    public OrderController() {
        log.info("OrderController 已初始化");
    }

    @Operation(summary = "创建订单", description = "创建新订单")
    @ApiResponse(responseCode = "200", description = "成功创建订单")
    @PostMapping
    public Order createOrder(@RequestBody Order order) {
        order.setOrderNo(orderService.generateOrderNo());
        orderService.save(order);
        return order;
    }

    @Operation(summary = "查询订单", description = "根据ID查询订单")
    @ApiResponse(responseCode = "200", description = "成功获取订单")
    @Cacheable(value = "dataCache", key = "#id")
    @GetMapping("/{id}")
    public Order getOrderById(@PathVariable Integer id) {
        log.info("查询订单ID: {}", id);
        return orderService.getById(id);
    }

    @Operation(summary = "更新订单", description = "更新订单信息")
    @ApiResponse(responseCode = "200", description = "成功更新订单")
    @CachePut(value = "dataCache", key = "#order.id")
    @PutMapping
    public boolean updateOrder(@RequestBody Order order) {
        return orderService.updateById(order);
    }

    @Operation(summary = "删除订单", description = "根据ID删除订单")
    @ApiResponse(responseCode = "200", description = "成功删除订单")
    @CacheEvict(value = "dataCache", key = "#id")
    @DeleteMapping("/{id}")
    public boolean deleteOrder(@PathVariable Integer id) {
        return orderService.removeById(id);
    }

    @Operation(summary = "分页查询订单", description = "分页获取订单列表")
    @ApiResponse(responseCode = "200", description = "成功分页获取订单")
    @GetMapping("/querypage/{page}/{size}")
    public List<Order> queryPage(@PathVariable("page") Integer page,
                                 @PathVariable("size") Integer size) {
        Page<Order> orderPage = new Page<>(page, size);
        return orderService.page(orderPage).getRecords();
    }

    @Operation(summary = "条件分页查询订单", description = "根据条件分页查询订单")
    @ApiResponse(responseCode = "200", description = "成功条件查询订单")
    @GetMapping("/querylist/{page}/{size}")
    public List<Order> queryPageByCondition(@PathVariable("page") Integer page,
                                            @PathVariable("size") Integer size,
                                            @RequestBody(required = false) Order order) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        if (order != null) {
            if (order.getOrderNo() != null) {
                queryWrapper.like("order_no", order.getOrderNo());
            }
            if (order.getUserId() != null) {
                queryWrapper.eq("user_id", order.getUserId());
            }
        }
        Page<Order> orderPage = new Page<>(page, size);
        return orderService.page(orderPage, queryWrapper).getRecords();
    }
}